package studykana.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import studykana.database.AppData;
import studykana.database.Databasehandler;
import toan.da.com.vn.R;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("HandlerLeak")
public class HocChuCaiActivity extends Activity {

	Databasehandler db;

	List<String> lstQuestion;

	private Button btnQuestion;
	private EditText etAnswer;
	private TextView tvMessage;
	private ImageButton btnHelp;
	private static final Random r = new Random();

	// menu = 0: hiragana, menu = 1: katakana
	private int menu;

	private ArrayList<Integer> listIdSelected;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.hoc_chu_cai_activity);

		ActionBar actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		listIdSelected = new ArrayList<Integer>();
		for (int i = 1; i < 47; i++) {
			listIdSelected.add(i);
		}
		menu = 0;
		Bundle b = getIntent().getExtras();
		if (b != null) {
			menu = b.getInt("menu");
			listIdSelected = b.getIntegerArrayList("listidselected");
		}

		btnQuestion = (Button) findViewById(R.id.btnAutoQuestion);
		etAnswer = (EditText) findViewById(R.id.etAnswer);
		tvMessage = (TextView) findViewById(R.id.tvMessage);
		btnHelp = (ImageButton) findViewById(R.id.btnHelp);
		db = new Databasehandler(this);

		createListQuestion();

		btnQuestion.setText(autoQuestion());

		// Even checking answer when user is entering...
		TextWatcher tw = new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {
				if (checkAnswer(etAnswer.getText().toString())) {
					DisplayMessage();
					btnQuestion.setText(autoQuestion());
					etAnswer.setText(null);
				}
			}
		};

		etAnswer.addTextChangedListener(tw);

		// when user click it, show answer.
		btnHelp.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				String answer = null;
				if (menu == 0) {
					answer = db.getRomajiByHira(btnQuestion.getText()
							.toString());
				} else
					answer = db.getRomajiByKata(btnQuestion.getText()
							.toString());
				Toast.makeText(getApplicationContext(), answer,
						Toast.LENGTH_LONG).show();
			}
		});
	}

	// Create random question
	private String autoQuestion() {
		return lstQuestion.get(r.nextInt(lstQuestion.size()));
	}

	// create list question. Default is level 1 (46 alphabets) but user can
	// custom
	private void createListQuestion() {
		lstQuestion = new ArrayList<String>();
		// menu = 0: study hiragana, menu = 1: study katakana
		switch (menu) {
		case 0:
			for (int i = 0; i < AppData.listAllAlphabet.size(); i++) {
				if (listIdSelected.contains(AppData.listAllAlphabet.get(i)
						.getId()))
					lstQuestion.add(AppData.listAllAlphabet.get(i)
							.getHiragana());
			}
			break;
		case 1:
			for (int i = 0; i < AppData.listAllAlphabet.size(); i++) {
				if (listIdSelected.contains(AppData.listAllAlphabet.get(i)
						.getId()))
					lstQuestion.add(AppData.listAllAlphabet.get(i)
							.getKatakana());
			}
			break;
		default:
			break;
		}

	}

	// check answer true or false.
	private boolean checkAnswer(String answer) {
		if (menu == 0) {
			if (answer.equals(db.getRomajiByHira(btnQuestion.getText()
					.toString())))
				return true;
			else
				return false;
		} else {
			if (answer.equals(db.getRomajiByKata(btnQuestion.getText()
					.toString())))
				return true;
			else
				return false;
		}
	}

	// Show message "correct" when answer is true.
	protected void DisplayMessage() {

		tvMessage.setVisibility(TextView.VISIBLE);
		new Thread() {
			public void run() {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
				}
				messageHandler.sendEmptyMessage(0);
			}
		}.start();
	}

	private Handler messageHandler = new Handler() {

		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			tvMessage.setVisibility(TextView.INVISIBLE);
		}
	};

	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		getMenuInflater().inflate(R.menu.menu_hocchucai, menu);
		return true;

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		// go to alphabet list screen
		case R.id.menu_table:
			Intent intentSetting = new Intent(this, BangChuCaiActivity.class);
			intentSetting.putIntegerArrayListExtra("listidselected",
					listIdSelected);
			intentSetting.putExtra("menu", menu);
			startActivity(intentSetting);
			finish();
			break;
		// back to home
		case android.R.id.home:
			Intent intentHome = new Intent(this, MainActivity.class);
			startActivity(intentHome);
			finish();
			break;
		// change study hiragana or katakana
		case R.id.menu_change:

			if (menu == 0) {
				menu = 1;
				createListQuestion();
				Toast.makeText(this, "Bạn đã đổi qua chữ Katakana",
						Toast.LENGTH_SHORT).show();
			} else {
				menu = 0;
				createListQuestion();
				Toast.makeText(this, "Bạn đã đổi qua chữ Hiragana",
						Toast.LENGTH_SHORT).show();
			}
			btnQuestion.setText(autoQuestion());
			break;
		default:
			break;
		}
		return true;
	}
}
